package com.kj.platformlogistics.my;

import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.kj.platformlogistics.R;
import com.kj.platformlogistics.base.BaseVPFragment;
import com.kj.platformlogistics.common.MethodUtils;
import com.kj.platformlogistics.common.UserData;
import com.kj.platformlogistics.constants.UrlConstants;
import com.kj.platformlogistics.http.HttpRequest;
import com.kj.platformlogistics.http.HttpStringCallBack;
import com.kj.platformlogistics.login.LoginActivity;
import com.kj.platformlogistics.my.activity.ActivityIdentity;
import com.kj.platformlogistics.my.activity.ActivityPersonInfo;
import com.kj.platformlogistics.my.model.UserInfoModel;
import com.kj.platformlogistics.util.JsonUtils;
import com.kj.platformlogistics.util.PictureUtils;
import com.kj.platformlogistics.util.SPUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class MyIndex  extends BaseVPFragment implements View.OnClickListener {

    private Context context;
    private View view;
    private LinearLayout llPersonInfo,llIdentity,llUserInfo,llPersonInfo1;
    private TextView tvUserName,tvTip,tvUserPhone,tvIsIdentify;
    private ImageView ivHeadLink;
    private TextView tvLogin;

    private Boolean isLogin=true;


    public static MyIndex newInstance(String content) {
        Bundle arguments = new Bundle();
        arguments.putString("string", content);
        MyIndex fl = new MyIndex();
        fl.setArguments(arguments);
        return fl;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.my_index, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }


    public void initView(){
        ivHeadLink=view.findViewById(R.id.ivHeadLink);
        llPersonInfo=view.findViewById(R.id.llPersonInfo);
        llIdentity=view.findViewById(R.id.llIdentity);
        tvUserName=view.findViewById(R.id.tvUserName);
        llUserInfo=view.findViewById(R.id.llUserInfo);
        llPersonInfo1=view.findViewById(R.id.llPersonInfo1);
        tvLogin=view.findViewById(R.id.tvLogin);
        tvUserPhone=view.findViewById(R.id.tvUserPhone);
        tvTip=view.findViewById(R.id.tvTip);
        tvIsIdentify=view.findViewById(R.id.tvIsIdentify);
        llIdentity.setOnClickListener(this::onClick);
        llPersonInfo.setOnClickListener(this::onClick);
        tvLogin.setOnClickListener(this::onClick);
        llPersonInfo1.setOnClickListener(this::onClick);

        if (SPUtils.getSharedBooleanData(context,"isLogin")){
            //已经登陆了
            isLogin=true;
            tvLogin.setText("退出登录");
        }else {
            isLogin=false;
            tvLogin.setText("登录");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tvLogin:
                if (isLogin){
                    //退出登录
                    MethodUtils.logoutAfter(context);
                    tvUserName.setText("点击登录");
                    tvLogin.setText("登录");
                    tvTip.setVisibility(View.VISIBLE);
                    llUserInfo.setVisibility(View.GONE);
                    ivHeadLink.setImageResource(R.mipmap.default_headlink);
                }
                LoginActivity.actionStart(context);
                isLogin=!isLogin;
                break;
            case R.id.llPersonInfo:
            case R.id.llPersonInfo1:
                ActivityPersonInfo.actionStart(context);
                break;
            case R.id.llIdentity:
                //身份认证
                ActivityIdentity.actionStart(context);
                break;
        }
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (isVisible) {
            if (SPUtils.getSharedBooleanData(context,"isLogin")){
                getPersonInfo();
            }else {
                LoginActivity.actionStart(context);
            }
        }
    }

    private void getPersonInfo() {
        new HttpRequest(getContext()).doPost(UrlConstants.logistics_getStationUserInfo, null, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                try {
                    String data = result.toString();
                    //把请求得数据转换成json对象
                    JSONObject jsonObject = null;
                    jsonObject = new JSONObject(data);
                    String message = jsonObject.getString("message");
                    Boolean success=jsonObject.getBoolean("success");
                    if(success){
                        Object obj = JsonUtils.fromJson(result.toString(), UserInfoModel.class);
                        if (obj instanceof UserInfoModel) {
                            UserInfoModel model = (UserInfoModel) obj;
                            UserData.create(context).saveUserData(model.getResult());

                                tvUserName.setText(UserData.create(context).get(UserData.REALNAME));
                                tvTip.setVisibility(View.GONE);
                                llUserInfo.setVisibility(View.VISIBLE);
                                tvUserPhone.setText(UserData.create(context).get(UserData.PHONE));
                                Glide.with(context).load(UserData.create(context).get(UserData.ICON))
                                        .placeholder(R.mipmap.default_headlink)
                                        .apply(new RequestOptions().transform(new PictureUtils()))
                                        .into(ivHeadLink);
                                if (UserData.create(context).get(UserData.ISIDENTITY) != null &&
                                        UserData.create(context).get(UserData.ISIDENTITY).equals("0")) {
                                    tvIsIdentify.setText("未认证");
                                } else {
                                    tvIsIdentify.setText("已认证");
                                }
                        }
                    }else {
                        Toast.makeText(getContext(),message,Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) {
                Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
            }
        });
    }


    @Override
    protected void onFragmentFirstVisible() { }
}